package com.szqhyxj.youxiangju.biz.equipmentActivation.buySlots;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.merchantHome.shop.pay.AliPayActivity;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by leung on 2018/11/9.
 */

public class BuySlotsActivity extends BaseActivity implements BuySlotContract.View, NavigationBar.NavigationBarListener {
    static String slotsnum;
    static String deviceNo;
    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.totalprice)
    TextView totalprice;
    @BindView(R.id.less)
    TextView less;
    @BindView(R.id.num)
    TextView num;
    @BindView(R.id.plus)
    TextView plus;
    @BindView(R.id.pay_slot)
    TextView paySlot;

    BuySlotPresenter presenter;

    public static void start(Activity activity, String slots, String deviceno) {
        Intent intent = new Intent();
        intent.setClass(activity, BuySlotsActivity.class);
        activity.startActivity(intent);
        slotsnum = slots;
        deviceNo = deviceno;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buy_slots);
        ButterKnife.bind(this);
        num.setText("1");
        totalprice.setText("50.00");
        presenter = new BuySlotPresenter(this);
        navigationBar.setNavigationBarListener(this);

    }

    @OnClick({R.id.less, R.id.plus, R.id.pay_slot})
    public void onClick(View view) {
        int nums = Integer.parseInt(num.getText().toString());
        int slotnum = Integer.parseInt(slotsnum);
        switch (view.getId()) {
            case R.id.less:
                if (nums <= 1) {
                    totalprice.setText(Integer.parseInt(num.getText().toString()) * 50 + ".00");
                    Toast.makeText(this, "已是最少数量了，不能再少了哦！", Toast.LENGTH_SHORT).show();
                }
                if (1 < nums && nums <= slotnum) {
                    int a = nums - 1;
                    num.setText(a + "");
                    totalprice.setText(Integer.parseInt(num.getText().toString()) * 50 + ".00");
                }
                break;
            case R.id.plus:
                if (nums >= slotnum) {
                    totalprice.setText(Integer.parseInt(num.getText().toString()) * 50 + ".00");
                    Toast.makeText(this, "已是最大数量了，不能再多了哦！", Toast.LENGTH_SHORT).show();
                }
                if (1 <= nums && nums < slotnum) {
                    int a = nums + 1;
                    num.setText(a + "");
                    totalprice.setText(Integer.parseInt(num.getText().toString()) * 50 + ".00");
                }
                break;
            case R.id.pay_slot:
                presenter.getOrder(deviceNo, Integer.parseInt(num.getText().toString()));
                break;
        }
    }


    @Override
    public void navtoApli(String orderno) {
        AliPayActivity.start(this, orderno, 2);

    }

    @Override
    public void navigationLeft() {
        finish();
    }

    @Override
    public void navigationRight() {

    }
}
